package com.lebang.service.cences;

import com.lebang.domain.CencesCommonSdk;
import com.lebang.example.CencesCommonSdkExample;
import com.lebang.mapper.CencesCommonSdkMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.mapper.MemberNewTradeMapper;
import com.lebang.model.MyConfig;
import com.lebang.model.Whether;
import com.lebang.model.cences.CencesMsg;
import com.lebang.model.cences.MemberNewTradeCount;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新手任务
 *
 * @Author xwj
 * @Date 2020-04-09
 */
@Service
public class CencesCommonSdkService extends BaseService<CencesCommonSdk, Integer, CencesCommonSdkExample> {
    @Autowired
    private CencesCommonSdkMapper baseMapper;

    @Autowired
    private LbbMemberMapper lbbMemberMapper;

    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;

    @Autowired
    private MemberNewTradeMapper memberNewTradeMapper;

    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    /**
     * 统计指定日期的自营SDK
     *
     * @param end   结束日期
     * @param brand 渠道
     */
    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime = System.currentTimeMillis();
        //star 开始日期  end 结束日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));
        LocalDateTime star = end.minusDays(1);
        //所有的渠道来源
        if (brand == null) {
            brand = lbbMemberMapper.selectBrand();
        }

        //通用任务,试玩任务
        List<String> newPayList = new ArrayList<>();
        newPayList.add(TaskCategory.Common.name());
        newPayList.add(TaskCategory.AoTian.name());
        newPayList.add(TaskCategory.XianWan.name());
        newPayList.add(TaskCategory.PCDANDAN.name());
        newPayList.add(TaskCategory.TAOJIN.name());
        newPayList.add(TaskCategory.LEXIANG.name());
        newPayList.add(TaskCategory.JUXIANGWAN.name());
        newPayList.add(TaskCategory.DANDAN_ZHUAN.name());
        newPayList.add(TaskCategory.YW_HZ.name());

        //根据任务类型付费额(sdk+截图)
        List<MemberNewTradeCount> memberNewTradeMoneys = memberNewTradeMapper.sumByDate(star, end, newPayList);
        memberNewTradeMoneys.forEach(s -> {
            s.setIsNewMemberAppstore(s.getIsNewMember() + ":" + s.getAppStore());
        });
        Map<String, List<MemberNewTradeCount>> memberNewTradeMap = memberNewTradeMoneys.stream().collect(Collectors.groupingBy(MemberNewTradeCount::getIsNewMemberAppstore));

        CencesCommonSdk cencesCommonSdk = new CencesCommonSdk(time, MyConfig.getEnv());
        brand.forEach(s -> {
            //统计sdk+截图
            myMoney(s, memberNewTradeMap, cencesCommonSdk);
        });

        baseMapper.insertCommonSdk(cencesCommonSdk);


        cencesMsgMongodb.save(new CencesMsg("自营SDK营收统计", (System.currentTimeMillis() - starTime)));
    }


    /**
     * 统计渠道活跃人数
     *
     * @param appStore 渠道
     */
    private void myMoney(String appStore, Map<String, List<MemberNewTradeCount>> memberNewTradeMap, CencesCommonSdk cencesCommonSdk) {
        switch (appStore) {
            case "vivo":
                cencesCommonSdk.setVivo(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            case "oppo":
                cencesCommonSdk.setOppo(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            case "huawei":
                cencesCommonSdk.setHuawei(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            case "xiaomi":
                cencesCommonSdk.setXiaomi(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            case "meizu":
                cencesCommonSdk.setMeizu(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            case "360":
                cencesCommonSdk.setSll(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            case "yingyongbao":
                cencesCommonSdk.setYyb(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            case "xiaozhuozhangshuang":
                cencesCommonSdk.setXiaozhuo(sumMoneyByMap(memberNewTradeMap, Whether.YES.getDbIndex(), appStore).add(sumMoneyByMap(memberNewTradeMap, Whether.NO.getDbIndex(), appStore)));
                break;
            default:
                break;
        }
    }

    /**
     * 统计任务收入
     *
     * @param memberNewTradeMap 统计数据
     * @param isNewMember       新老用户
     * @param appStore          渠道
     * @return 返回内容
     */
    private BigDecimal sumMoneyByMap(Map<String, List<MemberNewTradeCount>> memberNewTradeMap, Integer isNewMember, String appStore) {
        BigDecimal sumMoney = BigDecimal.ZERO;
        List<MemberNewTradeCount> memberNewTradeCounts = memberNewTradeMap.get(isNewMember + ":" + appStore);
        if (null == memberNewTradeCounts) {
            return sumMoney;
        }
        for (MemberNewTradeCount memberNewTradeCount : memberNewTradeCounts) {
            sumMoney = sumMoney.add(memberNewTradeCount.getMoney());
        }
        return sumMoney;
    }

    public void deleteByTime(Integer time) {
        CencesCommonSdkExample cencesCommonSdkExample = new CencesCommonSdkExample();
        cencesCommonSdkExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        baseMapper.deleteByExample(cencesCommonSdkExample);
    }
}
